package com.EAsystem.service.Impl;

import com.EAsystem.entity.Evaluation;
import com.EAsystem.mapper.EvaluationMapper;
import com.EAsystem.service.EvaluationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class EvaluationServiceImpl implements EvaluationService {

    @Autowired
    private EvaluationMapper evaluationMapper;

    @Override
    @Transactional
    public boolean addEvaluation(Evaluation evaluation) {
        Date currentTime = new Date();
        evaluation.setCreateTime(currentTime);
        evaluation.setUpdateTime(currentTime);
        return evaluationMapper.insertEvaluation(evaluation) > 0;
    }

    @Override
    @Transactional
    public boolean deleteEvaluationById(Integer id) {
        return evaluationMapper.deleteEvaluationById(id) > 0;
    }

    @Override
    @Transactional
    public boolean updateEvaluation(Evaluation evaluation) {
        evaluation.setUpdateTime(new Date());
        return evaluationMapper.updateEvaluation(evaluation) > 0;
    }

    @Override
    public Evaluation getEvaluationById(Integer id) {
        return evaluationMapper.findEvaluationById(id);
    }



    @Override
    public PageInfo<Evaluation> getAllEvaluationsByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Evaluation> evaluations = evaluationMapper.findAllEvaluationsWithPage();
        return new PageInfo<>(evaluations);
    }

    @Override
    public List<Evaluation> getEvaluationsByStudentId(Integer studentId) {
        return evaluationMapper.findEvaluationsByStudentId(studentId);
    }


    @Override
    public Evaluation getEvaluationByStudentAndCourse(Integer studentId, Integer courseId) {
        return evaluationMapper.findEvaluationByStudentAndCourse(studentId, courseId);
    }

    @Override
    public PageInfo<Evaluation> findEvaluationsByCondition(Integer pageNum, Integer pageSize, Evaluation evaluation) {
        PageHelper.startPage(pageNum, pageSize);
        List<Evaluation> evaluations = evaluationMapper.findEvaluationsByCondition(evaluation);
        return new PageInfo<>(evaluations);
    }
}